package com.typing.battle.activity;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

import com.typing.battle.R;
import com.typing.battle.listener.OnDataSyncCompleteListener;
import com.typing.battle.util.PreferenceMgr;
import com.typing.battle.util.ServerComunicationUtil;
import com.typing.battle.value.ServerComunicationDTO;

public class TypingBattleFriendAddActivity extends TypingBattleRootActivity {
    
	private String _ID;
	private Button add,back;
	private EditText friendEmail;
	public Context mContext;
	
	private PreferenceMgr mgr;
    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.battle_friend_add);	     
        mContext = this;
       
        mgr = PreferenceMgr.getInstance(this);
        friendEmail = (EditText)findViewById(R.id.input_friend_email);        
        friendEmail.setText("kukbab1@gmail.com");
		add = (Button)findViewById(R.id.btn_add_friend);
		add.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ServerComunicationDTO dto = new ServerComunicationDTO();
				dto.setActionType(ServerComunicationUtil.TYPE_FRIEND_INSERT);
				dto.setResponseType(ServerComunicationUtil.RETURN_TYPE_STRING);
				setFriendData(dto);
				try{
					ServerComunicationUtil.serverAction(dto, mContext,TypingBattleFriendAddActivity.this);
				}catch(Exception e){
					Log.e("", "TYPE_FRIEND_INSERT ERROR ::: "+e.getMessage());
				}
			}
		});
		back = (Button)findViewById(R.id.btn_back);
		back.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.putExtra("RESULT", false);		
				setResult(RESULT_OK,intent);
				finish();
			}
		});
    } 
    public void setFriendData(ServerComunicationDTO dto){
    	friendEmail = (EditText)findViewById(R.id.input_friend_email);        
    	dto.setFriendEmail(friendEmail.getText().toString());
    	dto.setUserEmail(mgr.getData(PreferenceMgr.USER_EMAIL_KEY, ""));    	
    }
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add("방만들기");
		return super.onCreateOptionsMenu(menu);
	}
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		return super.onOptionsItemSelected(item);
	}
	@Override
	public void onSyncComplete(int resultCode, ServerComunicationDTO dto, Object resultObj) {
		if(resultCode == OnDataSyncCompleteListener.RESULT_SYNC_FAIL){
			if(resultObj!=null)
				ServerComunicationUtil.showNotify((String)resultObj, this);
		}else{
			if(dto.getActionType() == ServerComunicationUtil.TYPE_FRIEND_INSERT){
				String result = null;
				try{
					result = (String)resultObj;
					if("NOT MEMBER".equals(result)){
						Toast toast = Toast.makeText(mContext, "회원이 아닙니다." ,Toast.LENGTH_SHORT);
						toast.show();
					}else if("ALREADY FRIEND".equals(result)){
						Toast toast = Toast.makeText(mContext, "이미 친구입니다." ,Toast.LENGTH_SHORT);
						toast.show();
					}else{
						Log.d("json",result);
						JSONObject obj = new JSONObject(result);
						Intent intent = new Intent(mContext, TypingBattleFriendListActivity.class);
						intent.putExtra("friendEmail", obj.getString("userEmail"));
						intent.putExtra("friendName", obj.getString("userName"));
						intent.putExtra("friendPhoneNo", obj.getString("userPhoneNo"));
						setResult(TypingBattleFriendListActivity.CONTACT_PICKER,intent);
						//finish();
					}
				}catch(Exception e){
					
				}
			}		
		}
		super.onSyncComplete(resultCode, dto, resultObj);
	}
}